<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class autocomplete extends CI_Controller {

	function index()
	{
		
		$this->load->view('common/header');
				$this->load->view('autocomplete');

		$this->load->view('common/footer');
	}

	
function suggestions()
{
         $username = trim($this->input->get('term', TRUE)); //get term parameter sent via text field. Not sure how secure get() is
        $this->db->select('code, name,selling_price'); 
        $this->db->from('product');
        $this->db->like('name', $username);
        $query = $this->db->get();

        if ($query->num_rows() > 0) 
        {
            $data['response'] = 'true'; //If username exists set true
            $data['message'] = array(); 

            foreach ($query->result() as $row)
            {
                $data['message'][] = array(  
                    'label' => $row->name,
                    'value' => $row->name,
                    'user_id'  => $row->code,
					'selling_price'  => $row->selling_price
                );
            }    
        } 
        else
        {
            $data['response'] = 'false'; //Set false if user not valid
        }

        echo json_encode($data);
} 

function get_product(){
		$product_name = trim($_REQUEST['product_name']);
		$this->db->select('id,code, name,selling_price,purchase_price'); 
        $this->db->from('product');
        $this->db->like('name', $product_name);
        $query = $this->db->get();
		$product_list = $query->result_array();
	
		$product_data = '';
		
		foreach($product_list as $product){
			$product_data =  $product_data."<input type='hidden' id='price{$product['id']}' value='{$product['purchase_price']}' > <input type='hidden' id='rate{$product['id']}' value='{$product['selling_price']}' >  <input type='hidden' id='cod{$product['id']}' value='{$product['code']}' ><input type='hidden' id='unique{$product['id']}'  value='{$product['name']}'   ><p  id='{$product['id']}' value='name' class='display_box' tabindex='0'  onfocus='showFocus(this)' onKeydown='Javascript: if (event.keyCode==13) set_product(this.id)' onclick='set_product(this.id)'   >".$product['name']."</p>";

		}			

	echo  $product_data;

}


function get_product1(){ 
		$counter =  $_REQUEST['counter'];
		$product_name = trim($_REQUEST['product_name']);
		$this->db->select('id,code, name,selling_price,purchase_price'); 
        $this->db->from('product');
        $this->db->like('name', $product_name);
        $query = $this->db->get();
		$product_list = $query->result_array();
		
		$product_data = '';
		foreach($product_list as $product){
		$product_data =  $product_data."<input type='hidden' id='price{$product['id']}' value='{$product['purchase_price']}' > <input type='hidden' id='rate{$product['id']}' value='{$product['selling_price']}' >  <input type='hidden' id='cod{$product['id']}' value='{$product['code']}' ><input type='hidden' id='unique{$product['id']}'  value='{$product['name']}'   ><p  id='{$product['id']}' style='scroll';value='name' class='display_box' tabindex='0' onKeydown='Javascript: if (event.keyCode==13) set_product1(this.id,$counter)' onclick='set_product1(this.id,$counter)'   >".$product['name']."</p>";	
		}			

	echo $product_data;exit;

}


}